using System;
using System.Net;
using System.Net.Mail;
using System.Resources;
using System.Text;
using System.Web;
using System.Web.UI;
using newtelligence.DasBlog.Runtime;
using newtelligence.DasBlog.Web.Core;

namespace newtelligence.DasBlog.Web
{
    public partial class EmailBox : UserControl
    {
        private SharedBasePage requestPage;
        protected ResourceManager resmgr;

        protected void Page_Load(object sender, EventArgs e)
        {
            requestPage = Page as SharedBasePage;

            // if you are commenting on your own blog, no need for Captha
            if (SiteSecurity.IsValidContributor())
            {
                CaptchaControl1.Enabled = CaptchaControl1.Visible = false;
            }
            else
            {
                CaptchaControl1.Enabled = CaptchaControl1.Visible = requestPage.SiteConfig.EnableCaptcha;
            }

            resmgr = ApplicationResourceTable.Get();

            if (!IsPostBack)
            {
                if (Request.Cookies["name"] != null)
                {
                    string nameStr = HttpUtility.UrlDecode(Request.Cookies["name"].Value, Encoding.UTF8);
                    //truncate at 32 chars to avoid abuse...
                    name.Text = nameStr.Substring(0, Math.Min(32, nameStr.Length));
                }

                if (Request.Cookies["email"] != null)
                {
                    email.Text = HttpUtility.UrlDecode(Request.Cookies["email"].Value, Encoding.UTF8);
                }
            }

            DataBind();
        }


        protected void mailSend_Click(object sender, EventArgs e)
        {
            if (CaptchaControl1.Enabled && requestPage.SiteConfig.EnableCaptcha)
            {
                if (CaptchaControl1.UserValidated == false)
                {
                    return;
                }
            }

            if (Page.IsValid)
            {
                if (rememberMe.Checked)
                {
                    string path = HttpRuntime.AppDomainAppVirtualPath;

                    //We don't encode the name so High Latin folks like Ren Drie?el don't break
                    // the Http Input Validation stuff.
                    HttpCookie cookieName = new HttpCookie("name", name.Text);
                    cookieName.Path = path;
                    Response.Cookies.Add(cookieName);

                    HttpCookie cookieEmail = new HttpCookie("email", HttpUtility.HtmlEncode(email.Text));
                    cookieEmail.Path = path;
                    Response.Cookies.Add(cookieEmail);

                    Response.Cookies["name"].Expires = DateTime.MaxValue;
                    Response.Cookies["email"].Expires = DateTime.MaxValue;
                }


                if (requestPage.SiteConfig.SendCommentsByEmail &&
                    requestPage.SiteConfig.SmtpServer != null &&
                    requestPage.SiteConfig.SmtpServer.Length > 0)
                {
                    SendMailInfo defaultMailInfo = ComposeMail();

                    requestPage.DataService.RunActions(new object[] {defaultMailInfo});

                    string commentShort = defaultMailInfo.Message.Body.Replace("\n", " ");
                    if (commentShort.Length > 50)
                    {
                        commentShort = commentShort.Substring(0, 50) + "...";
                    }
                    requestPage.LoggingService.AddEvent(new EventDataItem(EventCodes.CommentEmail,
                                                                          commentShort,
                                                                          string.Format("{0} {1}",
                                                                                        defaultMailInfo.Message.From,
                                                                                        HttpUtility.HtmlEncode(name.Text))));
                }

                Response.Redirect(SiteUtilities.GetStartPageUrl(requestPage.SiteConfig), false);
            }
        }

        private SendMailInfo ComposeMail()
        {
            MailMessage emailMessage = new MailMessage();

            if (requestPage.SiteConfig.NotificationEMailAddress != null &&
                requestPage.SiteConfig.NotificationEMailAddress.Length > 0)
            {
                emailMessage.To.Add(requestPage.SiteConfig.NotificationEMailAddress);
            }
            else
            {
                emailMessage.To.Add(requestPage.SiteConfig.Contact);
            }

            emailMessage.Subject = String.Format
                ("Weblog Mail from '{0}' on '{1}'"
                 , HttpUtility.HtmlEncode(name.Text)
                 , HttpUtility.HtmlEncode(requestPage.SiteConfig.Title));

            emailMessage.Body = HttpUtility.HtmlEncode(comment.Text);

            emailMessage.IsBodyHtml = false;
            emailMessage.BodyEncoding = Encoding.UTF8;
            string from = HttpUtility.HtmlEncode(email.Text);

            if (from != null && from.Length > 0)
            {
                emailMessage.From = new MailAddress(from);
            }
            else
            {
                emailMessage.From = new MailAddress(requestPage.SiteConfig.Contact);
            }

            emailMessage.Headers.Add("Sender", requestPage.SiteConfig.Contact);

            // add the X-Originating-IP header
            string hostname = Dns.GetHostName();
            IPHostEntry ipHostEntry = Dns.GetHostEntry(hostname);

            if (ipHostEntry.AddressList.Length > 0)
            {
                emailMessage.Headers.Add("X-Originating-IP", ipHostEntry.AddressList[0].ToString());
            }

            SendMailInfo sendMailInfo = new SendMailInfo(emailMessage, requestPage.SiteConfig.SmtpServer,
                                                         requestPage.SiteConfig.EnableSmtpAuthentication,
                                                         requestPage.SiteConfig.UseSSLForSMTP,
                                                         requestPage.SiteConfig.SmtpUserName,
                                                         requestPage.SiteConfig.SmtpPassword,
                                                         requestPage.SiteConfig.SmtpPort);

            return sendMailInfo;
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
        }

        #endregion
    }
}